﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class AddFixedIncomePurchaseModel
    {
        public AddFixedIncomePurchaseModel(FixIncomeSecuritiesDTO headerDto)
        {
            Init();
            HeaderDto = headerDto;
        }
        public AddFixedIncomePurchaseModel()
        {
            Init();
        }
        private void Init()
        {
            if (null == Header)
                Header = new FixIncomeSecuritiesHeader();
            if (null == Purchase)
                Purchase = new FixincomesecuritiesPurchase();
            if (null == RtHistory)
                RtHistory = new RatingHistory();
            TransactionTypeList = new BindingList<SelectListItem>();
            TypeOfPaymentList = new BindingList<SelectListItem>();
            PhysicalList = new BindingList<SelectListItem>();
            KeepAtList = new BindingList<SelectListItem>();
            CounterPartyList = new BindingList<SelectListItem>();
            CurrencyList = new BindingList<SelectListItem>();
            RelatedOrNonRelatedList = new BindingList<SelectListItem>();
          //  AssignDefaultData();
        }
        public FixIncomePurchaseScreenData ScreenData;
        public void AssignComboboxData(FixIncomePurchaseScreenData screendata)
        {
            ScreenData = screendata;
            var defaultItem = WebUtil.SelectListSelectDefault();
            TypeOfPaymentList.Add(defaultItem);
            PhysicalList.Add(defaultItem);
            KeepAtList.Add(defaultItem);
            CounterPartyList.Add(defaultItem);
            CurrencyList.Add(defaultItem);
            RelatedOrNonRelatedList.Add(defaultItem);


            foreach (var item in screendata.CounterPartiesList)
            {
                  CounterPartyList.Add(item: new SelectListItem() { Selected = false, Text = item.CounterpartiesCodeName, Value = item.CounterpartiesId.ToString() });
            }

            foreach (var o in screendata.PaymentTypeList)
            {
                TypeOfPaymentList.Add(item: new SelectListItem() { Selected = false, Text = o.PaymentTypeName, Value = o.PaymentTypeId.ToString() });
            }



            //fixed value ตัวเลือก "Purchase", "Subscription"  Default = Purchase
            TransactionTypeList.Add(item: new SelectListItem() { Selected = false, Text = "Purchase", Value = "Purchase" });
            TransactionTypeList.Add(item: new SelectListItem() { Selected = false, Text = "Subscription", Value = "Subscription" });
            SelectedTransactionType = "Purchase";


            foreach (var item in screendata.PhysicalList)
            {
                PhysicalList.Add(item: new SelectListItem() { Selected = false, Text = item.ValueEN, Value = item.ValueEN.ToString() });
            }

            foreach (var item in screendata.KeepAtList)
            {
                KeepAtList.Add(item: new SelectListItem() { Selected = false, Text = item.ValueEN, Value = item.ValueEN.ToString() });
            }


            foreach (var item in screendata.CurrencyList)
            {
                CurrencyList.Add(item: new SelectListItem() { Selected = false, Text = item.CurrencySymbol, Value = item.CurrencySymbol.ToString() });
            }



            foreach (var item in screendata.RelateNonRelateList)
            {
                RelatedOrNonRelatedList.Add(item: new SelectListItem() { Selected = false, Text = item.ValueEN, Value = item.ValueEN.ToString() });
            }

         
        }

        #region main data
        public FixIncomeSecuritiesDTO HeaderDto
        {
            //get; set;
            set
            {
                if (value != null)
                    _headerDto = value;

                if (value.Header != null)
                    Header = value.Header;
               
                if (value.RatingHistory != null)
                    RtHistory = value.RatingHistory;
            }
            get
            {
                return _headerDto;
            }
        }

        public RatingHistory RtHistory { get; set; }
        public FixIncomeSecuritiesHeader Header { get; set; }
        public FixincomesecuritiesPurchase Purchase { get; set; }
        private FixIncomeSecuritiesDTO _headerDto { get; set; }
        #endregion

        #region dropdownlist data
        //TransactionType
        public string SelectedTransactionType
        {
            get { return Purchase.TransactionType; }
            set { Purchase.TransactionType = value; }
        }
        public IList<SelectListItem> TransactionTypeList { get; set; }

        //Type of Payment (.TYPEOFPAYMENT_NAME)
         [Required(ErrorMessage = "โปรดระบุ")]
        public int? SelectedTypeOfPayment
        {
            get { return Purchase.TypeofpaymentId; }
            set { Purchase.TypeofpaymentId = value; }
        }
        public IList<SelectListItem> TypeOfPaymentList { get; set; }

        //Physical
        public string SelectedPhysical
        {
            get { return Purchase.Physical; }
            set { Purchase.Physical = value; }
        }
        public IList<SelectListItem> PhysicalList { get; set; }

        //Keep at 
        public string SelectedKeepAt
        {
            get { return Purchase.KeepAt; }
            set { Purchase.KeepAt = value; }
        }
        public IList<SelectListItem> KeepAtList { get; set; }

        //CounterParty
         [Required(ErrorMessage = "โปรดระบุ")]
        public int? SelectedCounterParty
        {
            get { return Purchase.CounterpartyId; }
            set { Purchase.CounterpartyId = value; }
        }

       

        public IList<SelectListItem> CounterPartyList { get; set; }

        //Currency
        public string SelectedCurrency
        {
            get { return Purchase.Currency; }
            set { Purchase.Currency = value; }
        }
        public IList<SelectListItem> CurrencyList { get; set; }


        //Related / Non Related
        public string SelectedRelatedOrNonRelated
        {
            get { return Purchase.Related; }
            set { Purchase.Related = value; }
        }
        public IList<SelectListItem> RelatedOrNonRelatedList { get; set; }

        #endregion

        #region adapter data
        public int HeaderID 
        {
            get
            {
                return Header.FixincomesecuritiesHeaderId;
            }

            set
            {
                Header.FixincomesecuritiesHeaderId = value;
            }
        }
        public int PurchaseID
        {
            get
            {
                return Purchase.FixincomesecuritiesPurchaseId;
            }

            set
            {
                Purchase.FixincomesecuritiesPurchaseId = value;
            }
        }


        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Issue Date")]
        public DateTime? HeaderIssueDate
        {
            get { return Header.IssueDate; }
            set { Header.IssueDate = value; }
        }
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Maturity Date")]
        public DateTime? HeaderMaturityDate
        {
            get { return Header.MaturityDate; }
            set { Header.MaturityDate = value; }
        }
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Issue Rating Update Date")]
        public DateTime? RtHistoryIssueUpdateDate
        {
            get { return RtHistory.IssueUpdateDate; }
            set { RtHistory.IssueUpdateDate = value; }
        }
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Issuer Rating Update Date")]
        public DateTime? RtHistoryIssuerUpdateDate
        {
            get { return RtHistory.IssuerUpdateDate; }
            set { RtHistory.IssuerUpdateDate = value; }
        }
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Guarantor Rating Update Date")]
        public DateTime? RtHistoryGuarantorUpdateDate
        {
            get { return RtHistory.GuarantorUpdateDate; }
            set { RtHistory.GuarantorUpdateDate = value; }
        }


        [Required(ErrorMessage = "โปรดระบุ")]
        [Range((double)decimal.MinValue, (double)decimal.MaxValue, ErrorMessage = "ข้อมูลต้องเป็นตัวเลข")]
        public int? Unit
        {
            get { return Purchase.Unit; }
            set { Purchase.Unit = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public string RefNo
        {
            get { return Purchase.RefNo; }
            set { Purchase.RefNo = value; }
        }
        [Required(ErrorMessage = "โปรดระบุ")]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Trade Date")]
        public DateTime? TradeDate
        {
            get { return Purchase.TradeDate; }
            set { Purchase.TradeDate = value; }
        }
        [Required(ErrorMessage = "โปรดระบุ")]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Settlement Date")]
        public DateTime? SettlementDate
        {
            get { return Purchase.SettlementDate; }
            set { Purchase.SettlementDate = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? Yield
        {
            get { return Purchase.Yield; }
            set { Purchase.Yield = value; }
        }


        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? CleanPrice
        {
            get { return Purchase.CleanPrice; }
            set { Purchase.CleanPrice = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? AI
        {
            get { return Purchase.AI; }
            set { Purchase.AI = value; }
        }

        #endregion


        public string TIME2MATURE { get; set; }

        public String SubmitBtn { get; set; }
       // public String PurchaseID { get; set; }
        public String CancelBtn { get; set; }

     
        public bool UpdateMode { get; set; }
    }
}